package com.wangjiyuan.shiwupai.ui.fragment.guangchi.pager;

import com.wangjiyuan.shiwupai.bean.guangchi.evaluation.EvaluationBean;
import com.wangjiyuan.shiwupai.bean.guangchi.first.FirstBean;
import com.wangjiyuan.shiwupai.bean.guangchi.food.FoodBean;

import java.util.Map;

import rx.Observer;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by wjy on 2016/11/30.
 */

public class PagerPresenter implements PagerContract.PagerPresenter {
    private PagerContract.PagerView mView;
    private PagerContract.PagerModel mModel;
    private Subscription subscribe;

    public PagerPresenter(PagerContract.PagerView view) {
        mView = view;
        mModel = new PagerModel();

    }

    @Override
    public void getEvaluation(Map<String, String> map) {
        subscribe = mModel.getEvaluation(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<EvaluationBean>() {
                    @Override
                    public void onCompleted() {
                        subscribe.unsubscribe();
                    }

                    @Override
                    public void onError(Throwable e) {
                        subscribe.unsubscribe();
                    }

                    @Override
                    public void onNext(EvaluationBean evaluationBean) {
                        mView.getEvaluation(evaluationBean.getFeeds());
                    }
                });
    }

    @Override
    public void getFood(Map<String, String> map) {
        subscribe = mModel.getFood(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FoodBean>() {
                    @Override
                    public void onCompleted() {
                        subscribe.unsubscribe();
                    }

                    @Override
                    public void onError(Throwable e) {
                        subscribe.unsubscribe();
                    }

                    @Override
                    public void onNext(FoodBean FoodBean) {
                        mView.getFood(FoodBean.getFeeds());
                    }
                });
    }

    @Override
    public void getFirst(Map<String, String> map) {
        subscribe = mModel.getFirst(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<FirstBean>() {
                    @Override
                    public void onCompleted() {
                        subscribe.unsubscribe();
                    }

                    @Override
                    public void onError(Throwable e) {
                        subscribe.unsubscribe();
                    }

                    @Override
                    public void onNext(FirstBean firstBean) {
                        mView.getFirst(firstBean.getFeeds());
                    }
                });
    }
}
